package com.example.sunny.utils

import android.app.Activity
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.example.sunny.R
import com.example.sunny.pojo.weather.DailyResponse

object UIUtils {
    fun replaceFragment(activity: FragmentActivity, container:Int, fragment:Fragment){
        val fragmentManager = activity.supportFragmentManager
        fragmentManager.beginTransaction().apply {
            replace(container,fragment)
            addToBackStack(null)
            commit()
        }
    }

    object InflateUI {
        val lifeIndexImageList = listOf<Int>(R.drawable.ic_cold,R.drawable.ic_dress,R.drawable.ic_purple,R.drawable.ic_car)
        val lifeIndexDescList = listOf<String>("感冒","穿衣","紫外线","洗车")
        fun inflateLifeIndex(view: View, daily: DailyResponse.Daily, position:Int){
            val cardList =ArrayList<View>().apply {
                add(view.findViewById(R.id.card1))
                add(view.findViewById<View>(R.id.card2))
                add(view.findViewById<View>(R.id.card3))
                add(view.findViewById<View>(R.id.card4))
            }
            for (i in cardList.indices){
                val lifeImage = cardList[i].findViewById<ImageView>(R.id.lifeImage)
                lifeImage.setImageResource(lifeIndexImageList[i])

                val lifeDesc = cardList[i].findViewById<TextView>(R.id.lifeDesc)
                lifeDesc.text = lifeIndexDescList[i]

                val lifeIndex = daily.lifeIndex
                val lifeInfo = cardList[i].findViewById<TextView>(R.id.lifeInfo)
                when(lifeIndexDescList[i]){
                    "感冒" -> lifeInfo.text = lifeIndex.coldRisk[position].desc
                    "穿衣" ->lifeInfo.text = lifeIndex.dressing[position].desc
                    "紫外线" ->lifeInfo.text = lifeIndex.ultraviolet[position].desc
                    "洗车" ->lifeInfo.text = lifeIndex.carWashing[position].desc
                }
            }
        }
    }
}